/*
 * Connection.h
 *
 *  Created on: May 11, 2014
 *      Author: root
 */

#ifndef CONNECTION_H_
#define CONNECTION_H_
#include <zmq.hpp>
#include <string>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <thread>
#include "../Message/Message.pb.h"
#include "../SatLogger/SatLogger/Logger.h"

#include "../StringUtils/StringUtils.h"

using namespace std ;
using namespace message;
using namespace zmq ;
using namespace LoggerNameSpace;

class Connection {

public:
	Connection ( int port , string host ) ;
	Connection ( int port  );
	~Connection () ;
	bool sendAndWaitForReceive ( Message request , Message * received ) ;
	bool send(Message message);
	bool receive (Message * received) ;
	bool sendAndWaitForReceiveWithTimeout ( Message request ,Message * received ,  int timeoutsec ) ;
	bool sendWithTimeout(Message message , int timeoutsec );
	bool receiveWithTimeout (Message * received, int timeoutsec ) ;

private:
	context_t *context ;
	socket_t *socket ;
	int connectionPort ;
	string connectionHost ;
	Logger * logger;

	bool send ( message_t *request ) ;
	message_t * createMessage_t(Message message);
	void setSocketTimeout(int opt, int timeoutsec);
	void logZMQException(string text, zmq::error_t& e );
	//void * sendHeader ( RequestHeader header ) ;
} ;

#endif /* CONNECTION_H_ */
